<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('setTitle'))
{
	function setTitle($eTitle, $eSearch)
	{
		$eTitle = trim($eTitle);
		$eSearch = trim($eSearch);
		if($eSearch != '') $eTitle = preg_replace ('/'.$eSearch.'/is', '<font color="#CC3300">'.$eSearch.'</font>', $eTitle);
		
		return $eTitle;
	}
}

if ( ! function_exists('gaPathLink'))
{
	function gaPathLink($val)
	{
		$htmlPath = '';
		
		$CI =& get_instance();
		$eColumn = $CI->thor->columns;
		
		$valExp = explode('.', $val);
		foreach($valExp as $key => $item)
		{
			foreach($eColumn as $item2)
			{
				if($item2->id == $item)
				{
					$valExp = explode('.', $item2->path . $item2->id. '.');
					$htmlLink = '';
					foreach($valExp as $key => $item3)
					{
						foreach($eColumn as $item4)
						{
							if($item4->id == $item3)
							{
								$htmlLink .= '/'. $item4->htmlname;
								break;
							}
						}
					}
					
					$htmlPath .= ' &raquo; <a href="'.site_url($htmlLink).'">'. $item2->title . '</a>';
					break;
				}
			}
		}
		
		return $htmlPath;
	}
}

if ( ! function_exists('gaPath'))
{
	function gaPath($path, $url = FALSE)
	{
		$htmlPath = '';
		
		$CI =& get_instance();
		
		$pathExp = explode('.', $path);
		foreach($pathExp as $key => $item)
		{
			$item = (int)$item;
			
			if($item > 0)
			{
				$eColumn = $CI->thor->get_file('columns/'.$item);
				
				if($url == FALSE)
				{
					$htmlPath .= ' &raquo; '. $eColumn['title'];
				}
				else
				{
					$htmlPath .= ' &raquo; <a href="'.$url.$item.'">'. $eColumn['title'] .'</a>';
				}
			}
		}
		
		return $htmlPath;
	}
}

if ( ! function_exists('gaHtmlPath'))
{
	function gaHtmlPath($val)
	{
		$htmlPath = '';
		
		$CI =& get_instance();
		$eColumn = $CI->thor->columns;
		
		$valExp = explode('.', $val);
		foreach($valExp as $key => $item)
		{
			foreach($eColumn as $item2)
			{
				if($item2->id == $item)
				{
					$htmlPath .= '/'. $item2->htmlname;
					break;
				}
			}
		}
		
		return $htmlPath;
	}
}

if ( ! function_exists('findPath'))
{
	function findPath($id, $type, $default = '')
	{
		$CI =& get_instance();
		$eColumn = $CI->thor->columns;
		
		foreach($eColumn as $item)
		{
			if($item->id == $id)
			{
				switch($type)
				{
					case 'path':
					  return $item->path . $item->id . '.';
					  break;
					
					case 'title':
					  return $item->title;
					  break;
				}
				
				break;
			}
		}
		
		return $default;
	}
}

if ( ! function_exists('gaTitle'))
{
	function gaTitle($num, $id, $default = '')
	{
		$htmlTitle = $default;
		
		$CI =& get_instance();
		$eColumn = $CI->thor->get_file('columns_' . $num);
		
		foreach($eColumn as $item)
		{
			if($item->id == $id)
			{
				$htmlTitle = $item->title;
				break;
			}
		}
		
		return $htmlTitle;
	}
}


if ( ! function_exists('cutstr'))
{
	//截取字符串
	function cutstr($string, $sublen)    
	{
		$CI =& get_instance();
		
		$string = strip_tags($string);
		$string = preg_replace ('/\n/is', '', $string);
		$string = preg_replace ('/  |　　/is', '', $string);
		$string = preg_replace ('/&nbsp;/is', '', $string);
		$string = preg_replace ('/"/is', '&quot;', $string);
		
		preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $t_string);   
		if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))." …";   
		else $string = join('', array_slice($t_string[0], 0, $sublen));
		
		return $string;
	} 
}

if ( ! function_exists('jsonEncode'))
{
	function jsonEncode($value)
	{
		$value = preg_replace('/\s+/is', '', $value);
		$value = json_encode($value);
		$value = trim($value);
		
		return $value;
	}
}

if ( ! function_exists('replaceExpress'))
{
	function replaceExpress($content)
	{
		$content = str_replace("[大笑]","<img src='../../images/others/face_1.png' width='30' height='30'/>",$content);
		$content = str_replace("[惊讶]","<img src='../../images/others/face_2.png' width='30' height='30'/>",$content);
		$content = str_replace("[害羞]","<img src='../../images/others/face_3.png' width='30' height='30'/>",$content);
		$content = str_replace("[哈切]","<img src='../../images/others/face_4.png' width='30' height='30'/>",$content);
		$content = str_replace("[白眼]","<img src='../../images/others/face_5.png' width='30' height='30'/>",$content);
		$content = str_replace("[大哭]","<img src='../../images/others/face_6.png' width='30' height='30'/>",$content);
		$content = str_replace("[偷笑]","<img src='../../images/others/face_7.png' width='30' height='30'/>",$content);
		$content = str_replace("[示爱]","<img src='../../images/others/face_8.png' width='30' height='30'/>",$content);
		
		return $content;
	}
}